package java111_2;

import java.io.*;
import java.util.*;

public class Day38 {

    //https://ac.nowcoder.com/acm/problem/219641
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), tmp = 0;
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        int[] dp = new int[n+1];
        // dp[i] 表示前 i 个数中的第二大者
        dp[2] = Math.min(arr[0], arr[1]);
        tmp = Math.max(arr[0], arr[1]);
        for (int i = 3; i <= n; i++) {
            if(arr[i-1] < dp[i-1]){
                dp[i] = dp[i-1];
            } else if (arr[i-1] > tmp) {
                dp[i] = tmp;
                tmp = arr[i-1];
            } else {
                dp[i] = arr[i-1];
            }
        }
        int q = in.nextInt();
        while (q-- > 0){
            int x = in.nextInt();
            System.out.println(dp[x]);
        }
    }


    // https://ac.nowcoder.com/acm/problem/221786  快速读入
    public static void main2(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] arr = new int[n][n];
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                arr[i][j] = in.nextInt();
            }
        }
        int q = in.nextInt(), nums1 = 0, nums2 = 0;
        while(q-- > 0){
            if(in.nextInt() == 1){
                nums1++;
            } else {
                nums2++;
            }
        }
        nums1 %= 2;
        nums2 %= 2;
        boolean[][] flag1 = new boolean[n][n];
        if(nums1 != 0){
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if(!flag1[i][j]) {
                        swap(arr, i, j, n - i - 1, n - j - 1);
                        flag1[i][j] = flag1[n - i - 1][n - j - 1] = true;
                    } else {
                        i = n;
                        break;
                    }
                }
            }
        }
        boolean[][] flag2 = new boolean[n][n];
        if(nums2 != 0){
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if(!flag2[i][j]) {
                        swap(arr, i, j, n - i - 1, j);
                        flag2[i][j] = flag2[n - i - 1][j] = true;
                    } else {
                        i = n;
                        break;
                    }
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();;
        }
    }
    public static void swap(int[][] arr, int x1, int y1, int x2, int y2){
        int tmp = arr[x1][y1];
        arr[x1][y1] = arr[x2][y2];
        arr[x2][y2] = tmp;
    }


    // https://www.nowcoder.com/practice/6a7b2b6c9e3a4f56b1db9f8ca08d889b?tpId=230&tqId=38958&ru=/exam/oj
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), k = in.nextInt();
        long[] arr = new long[n];
        for(int i = 0; i < n; i++){
            arr[i] = in.nextLong();
        }
        long[][] dp = new long[n+1][k];
        // dp[i][j] 表示从 前 i 个数中选，和 % k == j 的最大和；
        for(int i = 0; i <= n; i++){
            for(int j = 0; j < k; j++){
                dp[i][j] = (long)-1e16;
            }
        }
        dp[0][0] = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 0; j < k; j++){
                int tmp = (int)((j - arr[i-1] % k + k) % k);
                dp[i][j] = Math.max(dp[i-1][j], dp[i-1][tmp] + arr[i-1]);
            }
        }
        System.out.println(dp[n][0] > 0 ? dp[n][0] : -1);
    }
}